﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;

using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmPartCheckOut : BaseForm
    {
        public RoomInfo RoomInfo = null;
        private OrderInfo orderInfo = null;
        private decimal amountMoney = 0.0M;
        private List<ConsumerListInfo> consumerList = new List<ConsumerListInfo>();
        private List<ConsumerListInfo> RoomDayChargeList = new List<ConsumerListInfo>();
        public FrmPartCheckOut()
        {
            InitializeComponent();
        }

        private void btnCheckOut_Click(object sender, EventArgs e)
        {
            if (this.txtPayMoney.Text.Length == 0 )
            {
                MessageUtil.ShowTips("请输入支付金额");
                this.txtPayMoney.Focus();
                return;
            }

            try
            {
                decimal payMoney = Convert.ToDecimal(this.txtPayMoney.Text);
                if (payMoney <= 0M)
                {
                    MessageUtil.ShowTips("请输入正确的支付金额");
                    this.txtPayMoney.Focus();
                    return;
                }

                #region 增加部分结账
                ConsumerListInfo info = new ConsumerListInfo();
                info.ItemName = "部分结账";
                info.Amount = -payMoney;
                info.BeginTime = DateTime.Now;
                info.Creator = Portal.gc.LoginInfo.FullName;
                info.Discount = 1.0;
                info.DiscountPrice = -payMoney;
                info.EndTime = DateTime.Now;
                info.Price = -payMoney;
                info.Quantity = 1;
                info.RoomNo = RoomInfo.RoomNo;
                info.Waiter = "*";//保留
                info.Order_ID = orderInfo.ID;
                info.Account = orderInfo.BillNumber;

                BLLFactory<ConsumerList>.Instance.Insert(info); 
                #endregion

                //记录收银明细
                AddMoneyDetail(payMoney);

                BindData(RoomInfo);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 增加收银记录
        /// </summary>
        /// <returns></returns>
        private void AddMoneyDetail(decimal payMoney)
        {
            if (payMoney != 0M)
            {
                MoneyDetailInfo info = new MoneyDetailInfo();
                info.Cash = payMoney;
                info.CashierType = "部分结账收银";
                info.Creator = Portal.gc.LoginInfo.FullName;
                info.PayDate = DateTime.Now;
                info.Customer_ID = orderInfo.Customer_ID;
                info.CustomerName = orderInfo.CustomerName;
                info.RoomNo = orderInfo.RoomNo;
                info.PayType = "现金";
                info.Note = string.Format("[部分结账收银]房间号:{0}", orderInfo.RoomNo);

                BLLFactory<MoneyDetail>.Instance.Insert(info);
            }
        }

        private void BindData(RoomInfo info)
        {
            if (RoomInfo != null)
            {
                orderInfo = BLLFactory<Order>.Instance.GetCurrentOrder(info.RoomNo);
                if (orderInfo != null)
                {
                    this.Text = string.Format("{0} 部分结账", info.RoomNo);
                    this.groupConsumeList.Text = string.Format("{0} {1}",
                        info.RoomNo, "消费清单");

                    amountMoney = OnShowStatus(info);
                    this.lblAmount.Text = string.Format("消费总金额：{0:C}", amountMoney);
                }      
            }
        }

        private decimal OnShowStatus(RoomInfo roomInfo)
        {
            decimal allMoney = 0.0M;

            #region 更新消费记录

            consumerList.Clear();
            if (roomInfo != null)
            {


                consumerList = BLLFactory<Room>.Instance.GetGoodsConsumption(roomInfo.RoomNo);
                //if (orderInfo.CheckType == 0)//散客
                //{
                //    //先判断是否夜审过
                //    if (orderInfo.IsAudit == 1)
                //    {

                //        consumerList = BLLFactory<Room>.Instance.GetGoodsConsumption(roomInfo.RoomNo);

                //    }
                //    else //表示当天就退房
                //    {

                //        //RoomDayChargeList = BLLFactory<Room>.Instance.GetRoomConsumption(roomInfo.RoomNo);//日房费
                //       // consumerList.AddRange(RoomDayChargeList);
                //        consumerList.AddRange(BLLFactory<Room>.Instance.GetGoodsConsumption(roomInfo.RoomNo));

                //    }

                //}
                //else //团体
                //{

                //    if (teamVirtualOrder != null)
                //    {

                //        consumerList = BLLFactory<ConsumerList>.Instance.Find("   Account='" + teamVirtualOrder.BillNumber + "'  ");


                //    }


                //}


                this.lvwDetail.Items.Clear();
                int i = 1;
               // offerMoney = 0.0M;
                foreach (ConsumerListInfo info in consumerList)
                {
                    ListViewItem item = new ListViewItem(info.RoomNo);
                    item.SubItems.Add(info.ItemName);
                    item.SubItems.Add(info.Price.ToString("C2"));
                    item.SubItems.Add(info.Discount.ToString());
                    item.SubItems.Add(info.DiscountPrice.ToString("C2"));
                    //offerMoney += info.Price - info.DiscountPrice;//优惠金额
                    item.SubItems.Add((info.Price - info.DiscountPrice).ToString("C2"));
                    item.SubItems.Add(info.Quantity.ToString());
                    item.SubItems.Add(info.Amount.ToString("C2"));
                    item.SubItems.Add(info.BeginTime.ToString());
                    item.SubItems.Add(info.Creator);
                    if (info.IsCheckout == 0)
                    {
                        item.SubItems.Add("未结账");

                    }
                    else
                    {
                        item.SubItems.Add("已结账");

                    }
                    item.Tag = info;
                    if (info.Quantity < 0)
                    {
                        item.ForeColor = Color.Red;
                    }

                    this.lvwDetail.Items.Add(item);
                    if (info.IsCheckout == 0)
                    {
                        allMoney += info.Amount;

                    }
                    i++;
                }
            }
            #endregion

            return allMoney;
        }


        private void ShowRoomTreeView()
        {
            #region 项目类型信息明细类别

            this.treeView1.Nodes.Clear();
            this.treeView1.BeginUpdate();

            List<RoomTypeInfo> typeList = BLLFactory<RoomType>.Instance.GetAll();
            foreach (RoomTypeInfo typeInfo in typeList)
            {
                string nodeName = string.Format("{0}  {1}", typeInfo.Name, typeInfo.DayPrice.ToString("C2"));
                TreeNode typeNode = new TreeNode(nodeName, 0, 0);
                this.treeView1.Nodes.Add(typeNode);
                //typeNode.Tag = info;

                List<RoomInfo> roomList = BLLFactory<Room>.Instance.FindRooms(typeInfo.Name, RoomStatus.住净房.ToString());
                foreach (RoomInfo roomInfo in roomList)
                {
                    TreeNode subNode = new TreeNode(roomInfo.RoomNo, 1, 1);
                    subNode.Tag = roomInfo;
                    typeNode.Nodes.Add(subNode);
                }
            }
            this.treeView1.EndUpdate();
            this.treeView1.ExpandAll();

            #endregion
        }

        private void FrmPartCheckOut_Load(object sender, EventArgs e)
        {
            this.txtPayMoney.Focus();

            BindData(RoomInfo);
            ShowRoomTreeView();
        }

        //private decimal OnShowStatus(RoomInfo roomInfo)
        //{
        //    decimal allMoney = 0.0M;

        //    #region 更新消费记录
        //    if (roomInfo != null)
        //    {
        //        List<ConsumerListInfo> consumerList = BLLFactory<Room>.Instance.GetAllConsumption(roomInfo.RoomNo);

        //        this.lvwDetail.Items.Clear();
        //        int i = 1;
        //        foreach (ConsumerListInfo info in consumerList)
        //        {
        //            ListViewItem item = new ListViewItem(info.RoomNo);
        //            item.SubItems.Add(info.ItemName);
        //            item.SubItems.Add(info.Price.ToString("C2"));
        //            item.SubItems.Add(info.Discount.ToString());
        //            item.SubItems.Add(info.Quantity.ToString());
        //            item.SubItems.Add(info.Amount.ToString("C2"));
        //            item.SubItems.Add(info.BeginTime.ToString());
        //            item.SubItems.Add(info.Waiter);
        //            item.SubItems.Add(info.Creator);
        //            item.Tag = info;
        //            if (info.Quantity < 0)
        //            {
        //                item.ForeColor = Color.Red;
        //            }

        //            this.lvwDetail.Items.Add(item);
        //            allMoney += info.Amount;
        //            i++;
        //        }
        //    }
        //    #endregion

        //    return allMoney;
        //}

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = e.Node;
            if (selectedNode.Tag != null)
            {
                RoomInfo info = selectedNode.Tag as RoomInfo;
                if (info != null)
                {
                    BindData(info);
                    RoomInfo = info;//重新更新变量，方便结算
                }
            }
        }

        private void txtPayMoney_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnCheckOut_Click(null, null);
            }
        }
    }
}
